/**
 * Created on 26 dec. 2011 at 15:05:54 by HozakaN
 */
package fr.castorhoz.isi_connectivity_widget.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * @author HozakaN
 * 
 */
public class DataBaseHelper extends SQLiteOpenHelper {

	/**
	 * Nom du schema de base pour l'application
	 */
	public final static String SCHEMA_NAME = "isi-connectivity-widget";

	/**
	 * Numero de version du schema de l'application
	 */
	public final static int SCHEMA_VERSION = 1;

	/*
	 * Champs relatifs a l'enregistrement des widgets que contiennent les
	 * appWidgets
	 */
	public final static String APPWIDGETS_WIDGETS_TABLENAME = "appWidgetsWidgetsTableName";
	public final static String APPWIDGET_ID = "appWidgetsId";
	public final static String APPWIDGETS_WIDGETS_NAME = "appWidgetsWidgetsName";

	/**
	 * Nom de la table renfermant l'etat du network mobile
	 */
	public final static String MOBILEDATA_STATE_TABLENAME = "mobileDataWidgetState";
	public final static String MOBILEDATA_STATE_COLUMNNAME = "state";

	/**
	 * Nom de la table renfermant l'etat du gps
	 */
	public final static String GPS_STATE_TABLENAME = "GPSWidgetState";
	public final static String GPS_STATE_COLUMNNAME = "state";
	
	/**
	 * Nom de la table renfermant l'etat du wifi
	 */
	public final static String WIFI_STATE_TABLENAME = "wifiWidgetState";
	public final static String WIFI_STATE_COLUMNNAME = "state";

	private SQLiteDatabase db;

	public DataBaseHelper(Context context) {
		super(context, SCHEMA_NAME, null, SCHEMA_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.i(DataBaseHelper.class.toString(), "onCreate method called");
		Log.println(Log.INFO, DataBaseHelper.class.toString(),
				"onCreate method called");
		this.db = db;
		createAppWidgetWidgetsTable();
		createGPSStateTable();
		createMobileDataStateTable();
		createWifiStateTable();
	}

	private void createWifiStateTable() {
        db.execSQL("create table "+WIFI_STATE_TABLENAME+" (_id integer primary key autoincrement, "
                + "state text not null"
                + ");");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.d(this.getClass().getName(), "onUpgrade called");
		this.db = db;
		Log.i(this.getClass().getName(),
				"Mise a jour de la Base de donnees version " + oldVersion
						+ " vers " + newVersion);
		db.execSQL("DROP TABLE IF EXISTS " + MOBILEDATA_STATE_TABLENAME);
		db.execSQL("DROP TABLE IF EXISTS " + GPS_STATE_TABLENAME);
        db.execSQL("DROP TABLE IF EXISTS "+WIFI_STATE_TABLENAME);
		db.execSQL("DROP TABLE IF EXISTS " + APPWIDGETS_WIDGETS_TABLENAME);
		onCreate(db);
	}

	/**
	 * Puts in database the widgets contained by an appWidget
	 * 
	 * @param tableName
	 */
	public void createAppWidgetWidgetsTable() {
		if (db == null) {
			Log.e(this.getClass().getName(), "db is null");
		}
		db.execSQL("create table " + APPWIDGETS_WIDGETS_TABLENAME
				+ " (_id integer primary key autoincrement, "
				+ APPWIDGETS_WIDGETS_NAME	+ " text not null, "
				+ APPWIDGET_ID + " integer not null"
				+ ");");
	}

	public void dropTable(String tableName) {
		db.execSQL("DROP TABLE IF EXISTS " + tableName);
	}

	public void createMobileDataStateTable() {
		db.execSQL("create table " + MOBILEDATA_STATE_TABLENAME
				+ " (_id integer primary key autoincrement, "
				+ MOBILEDATA_STATE_COLUMNNAME + " text not null" + ");");
	}

	public void createGPSStateTable() {
		db.execSQL("create table " + GPS_STATE_TABLENAME
				+ " (_id integer primary key autoincrement, "
				+ GPS_STATE_COLUMNNAME + " text not null" + ");");
	}
}
